package asia.yuanding.wechat.web.controller;

import asia.yuanding.wechat.system.constant.SystemConstant;
import asia.yuanding.wechat.system.exception.BusinessException;
import asia.yuanding.wechat.system.util.R;
import asia.yuanding.wechat.system.util.ThreadUser;
import asia.yuanding.wechat.web.entity.User;
import asia.yuanding.wechat.web.entity.UserContact;
import asia.yuanding.wechat.web.entity.UserContactApply;
import asia.yuanding.wechat.web.service.UserContactApplyService;
import asia.yuanding.wechat.web.service.UserContactService;
import asia.yuanding.wechat.web.service.UserService;
import asia.yuanding.wechat.web.service.impl.UserServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: ContactController
 * @Author: YuanDing
 * @Date: 2025/7/27 10:57
 * @Description:
 */

@RestController
@RequestMapping("contact")
public class ContactController {

    @Resource
    private UserContactService userContactService;

    @Resource
    private UserService userService;

    @Resource
    private UserContactApplyService userContactApplyService;

    /**
     * 搜索好友
     * @param searchKey
     * @return
     */
    @GetMapping("/{searchKey}")
    public R search(@PathVariable String searchKey) {
        User user = userService.getOne(
                new LambdaQueryWrapper<User>()
                        .eq(User::getWeChatCode , searchKey)
                        .or()
                        .eq(User::getEmail , searchKey)
                        .or()
                        .eq(User::getUid , searchKey));

        if (user != null) {
            UserContact one = userContactService.lambdaQuery()
                    .eq(UserContact::getContactId, user.getUid())
                    .eq(UserContact::getUserId , ThreadUser.getUId())
                    .eq(UserContact::getStatus, SystemConstant.UserContactStatus.FRIEND)
                    .one();
            if (one != null) {
                user.setFriend(true);
            }

            UserServiceImpl.userDesensitization(user);
        }

        return R.ok(user);
    }


    @GetMapping("list/{uId}")
    public R list(@PathVariable("uId") String uid) {
        List<UserContact> user = userContactService.getUserFriendList(uid);
        return R.ok(user);
    }


    /**
     * 申请好友
     * @param apply
     * @return
     * @throws BusinessException
     */
    @PostMapping("apply")
    public R apply(@RequestBody UserContactApply apply) throws BusinessException {
        userContactService.apply(apply);
        return R.ok();
    }

    /**
     * 查询好友列表
     * @param uid
     * @return
     */
    @GetMapping("apply/list/{uid}")
    public R applyList(@PathVariable("uid") String uid) {
        List<UserContactApply> userList = userContactService.getApplyList(uid);
        return R.ok(userList);
    }

    /**
     * 处理好友申请
     * @param apply
     * @return
     * @throws BusinessException
     */
    @PostMapping("handleApply")
    public R handleApply(@RequestBody UserContactApply apply) throws BusinessException {

        userContactService.handleApply(apply);
        return R.ok();
    }

}
